using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_Confirm : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_Confirm
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExButton btnCancel { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnConfirm { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnClose { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textTitle { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textContent { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnCancel", out var __tbv0);
                this.btnCancel = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnConfirm", out var __tbv1);
                this.btnConfirm = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnClose", out var __tbv2);
                this.btnClose = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textTitle", out var __tbv3);
                this.textTitle = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textContent", out var __tbv4);
                this.textContent = __tbv4;
            }
            #endregion ObjectBinding Generate 
        }

        #endregion Template Generate,don't modify

        #region fields
        private System.Action<UIN_Confirm, NoticeResultType> _onHideSelf = null;
        private float ChooseTime = -1;
        private bool isShowOneBtnCancel = false;

        public int id { get; private set; }

        private CfgNotice _cfgNotice;
        private Action _onConfirm;
        private Action _onCancel;
        private Action _onClose;

        #endregion fields

        #region properties

        protected UIB_UIN_Confirm ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_Confirm();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.ui.btnClose.onClick.AddListener(this.OnClickClose);
            this.ui.btnConfirm.onClick.AddListener(this.OnClickConfirm);
            this.ui.btnCancel.onClick.AddListener(this.OnClickCancel);
        }

        protected override void OnShow()
        {
        }

        protected override void OnHide()
        {
            this.ClearData();
        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            this.ClearData();
        }

        #region methonds

        protected override void OnUpdate(float dt, float unscaledDt)
        {
            if (_cfgNotice.AutoChoose > 0)
            {
                if (this.ChooseTime <= UnityEngine.Time.realtimeSinceStartup)
                {
                    if (_cfgNotice.AutoChoose == 1 || string.IsNullOrEmpty(_cfgNotice.ButtonText1))
                    {
                        this.OnClickConfirm();
                    }
                    else
                    {
                        this.OnClickCancel();
                    }
                }
            }
        }

        /// <summary>
        /// 展示确认框
        /// </summary>
        /// <param name="id">如果外部UI一对一持有该控件，或不关心顺序/与其他数据的关系，id可传0</param>
        /// <param name="cfgNotice"></param>
        /// <param name="finalDesc"></param>
        /// <param name="callBtnConfirm"></param>
        /// <param name="callBtnCancel"></param>
        /// <param name="callBtnClose"></param>
        /// <param name="onHideSelf"></param>
        public void Show(int id, CfgNotice cfgNotice, string finalDesc, Action callBtnConfirm, Action callBtnCancel, Action callBtnClose, Action<UIN_Confirm, NoticeResultType> onHideSelf)
        {
            this.Show();
            this.id = id;
            this._cfgNotice = cfgNotice;
            this._onConfirm = callBtnConfirm;
            this._onCancel = callBtnCancel;
            this._onClose = callBtnClose;

            this._onHideSelf = onHideSelf;

            this.ChooseTime = -1;
            if (_cfgNotice.AutoChoose != 0)
            {
                this.ChooseTime = _cfgNotice.AutoChooseTime + UnityEngine.Time.realtimeSinceStartup;
            }

            bool isShowBtn1 = !string.IsNullOrEmpty(_cfgNotice.ButtonText1);
            bool isShowBtn2 = !string.IsNullOrEmpty(_cfgNotice.ButtonText2);
            this.isShowOneBtnCancel = (isShowBtn1 && !isShowBtn2);

            this.ui.btnClose.SetActive(_cfgNotice.IsShowClose);
            this.ui.btnCancel.SetActive(isShowBtn1);
            this.ui.btnConfirm.SetActive(isShowBtn2);

            if (isShowBtn1)
            {
                this.ui.btnCancel.text = _cfgNotice.ButtonText1;
            }
            if (isShowBtn2)
            {
                this.ui.btnConfirm.text = _cfgNotice.ButtonText2;
            }

            this.ui.textTitle.text = _cfgNotice.Title;
            this.ui.textContent.text = finalDesc;
        }

        private void ClearData()
        {
            this._cfgNotice = null;
            this._onConfirm = null;
            this._onCancel = null;
            this._onClose = null;
            this.ChooseTime = -1;
            this.isShowOneBtnCancel = false;
            this._onHideSelf = null;
            this.id = 0;
        }

        void OnClickConfirm()
        {
            this._onConfirm?.Invoke();
            this.CallOnHideSelf(NoticeResultType.Confirm);
        }

        void OnClickCancel()
        {
            var call = this._onCancel;
            if (call == null && this.isShowOneBtnCancel)
            {
                call = this._onConfirm;
            }
            call?.Invoke();
            this.CallOnHideSelf(NoticeResultType.Cancel);
        }

        void OnClickClose()
        {
            this._onClose?.Invoke();
            this.CallOnHideSelf(NoticeResultType.Close);
        }

        void CallOnHideSelf(NoticeResultType type)
        {
            this._onHideSelf?.Invoke(this, type);
        }

        #endregion methonds
    }
}
